package com.smit.utils;

/**
 * strng工具类
 */
public final class StrUtil {
    private static final String TAG = "StrUtil";
    private StrUtil() { }
    /**
     * 判断string是否为null,"","null"
     * @param value string
     * @return true为空，false不为空
     */
    public static boolean isNullOrEmpty(String value) {
        return (value == null) || (value.length() == 0) || (value.equals("null"));
    }

    /**
     * 连接两个字符串
     * @param a 字符串
     * @param b 字符串
     * @return 连接后的字符串
     */
    public static String[] concat(String[] a, String[] b) {
        String[] c = new String[a.length + b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }

    /**
     * 判断两个字符相等，但是非空
     * @param a 字符串a
     * @param b 字符串b
     * @return true相等且非空，false不相等或空
     */
    public static boolean equalAndNotNull(final String a, final String b) {
        if (isNullOrEmpty(a) || isNullOrEmpty(b)) {
            return false;
        }
        if (a.equals(b)) {
            return true;
        }
        return false;
    }

    /**
     * string转long
     * @param string 字符串
     * @return long数据
     */
    public static long parseLong(final String string) {
        try {
            return Long.parseLong(string);
        } catch (NumberFormatException e) {
            LogUtil.error(TAG, "parseLong: NumberFormatException string = " + string);
            //LogUtil.handleUncaughtException(LiveVideoApplication.getInstance(), e);
            return 0L;
        } catch (Exception e) {
            LogUtil.error(TAG, "parseLong: Exception string = " + string);
            //LogUtil.handleUncaughtException(LiveVideoApplication.getInstance(), e);
            return 0L;
        }
    }

    /**
     * string转long
     * @param string 字符串
     * @return int数据
     */
    public static int parseInt(final String string) {
        try {
            return Integer.parseInt(string);
        } catch (NumberFormatException e) {
            LogUtil.error(TAG, "parseInt: NumberFormatException string = " + string);
            //LogUtil.handleUncaughtException(LiveVideoApplication.getInstance(), e);
            return 0;
        } catch (Exception e) {
            LogUtil.error(TAG, "parseInt: Exception string = " + string);
            //LogUtil.handleUncaughtException(LiveVideoApplication.getInstance(), e);
            return 0;
        }
    }

    /**
     * string转int
     * @param string 字符串
     * @param  radix 基数
     * @return int数据
     */
    public static int parseInt(final String string, final int radix) {
        try {
            return Integer.parseInt(string, radix);
        } catch (NumberFormatException e) {
            LogUtil.error(TAG, "parseInt: NumberFormatException string = " + string + " radix = " + radix);
            //LogUtil.handleUncaughtException(LiveVideoApplication.getInstance(), e);
            return 0;
        } catch (Exception e) {
            LogUtil.error(TAG, "parseInt: Exception string = " + string + " radix = " + radix);
            //LogUtil.handleUncaughtException(LiveVideoApplication.getInstance(), e);
            return 0;
        }
    }
}
